package com.ruoyi.hospital.controller.member;

import java.util.List;

import com.ruoyi.system.domain.SysAddress;
import com.ruoyi.system.service.ISysAddressService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hospital.domain.member.MemberAddress;
import com.ruoyi.hospital.service.member.IMemberAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员收货地址Controller
 * 
 * @author wendao
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/hospital/memberAddress")
public class MemberAddressController extends BaseController
{
    @Autowired
    private IMemberAddressService memberAddressService;
    @Autowired
    private ISysAddressService iSysAddressService;
    /**
     * 查询会员收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberAddress memberAddress)
    {
        startPage();
        List<MemberAddress> list = memberAddressService.selectMemberAddressList(memberAddress);
        return getDataTable(list);
    }

    /**
     * 导出会员收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:export')")
    @Log(title = "会员收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberAddress memberAddress)
    {
        List<MemberAddress> list = memberAddressService.selectMemberAddressList(memberAddress);
        ExcelUtil<MemberAddress> util = new ExcelUtil<MemberAddress>(MemberAddress.class);
        util.exportExcel(response, list, "会员收货地址数据");
    }

    /**
     * 获取会员收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(memberAddressService.selectMemberAddressById(id));
    }

    /**
     * 新增会员收货地址
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:add')")
    @Log(title = "会员收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberAddress memberAddress)
    {
        memberAddress.setProvince(getName(memberAddress.getProvinceId()));
        memberAddress.setCity(getName(memberAddress.getCityId()));
        memberAddress.setDistrict(getName(memberAddress.getDistrictId()));
        return toAjax(memberAddressService.insertMemberAddress(memberAddress));
    }

    /**
     * 修改会员收货地址
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:edit')")
    @Log(title = "会员收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberAddress memberAddress)
    {
        memberAddress.setProvince(getName(memberAddress.getProvinceId()));
        memberAddress.setCity(getName(memberAddress.getCityId()));
        memberAddress.setDistrict(getName(memberAddress.getDistrictId()));
        return toAjax(memberAddressService.updateMemberAddress(memberAddress));
    }

    /**
     * 删除会员收货地址
     */
    @PreAuthorize("@ss.hasPermi('hospital:memberAddress:remove')")
    @Log(title = "会员收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(memberAddressService.deleteMemberAddressByIds(ids));
    }


    public  String getName(Integer id){
        SysAddress sysAddress = iSysAddressService.selectSysAddressById(id);
        if(sysAddress==null){
            return null;
        }else{
            return sysAddress.getName();
        }
    }
}
